/**
 * Created by yhg97p on 2016/9/27.
 */
define(['app', 'filters/filters'], function(app) {
	app.register.controller('taskCourse', function($scope, $http, $state, $sce, $stateParams, $rootScope,$timeout,$cookieStore) {
        $scope.loading_show = true;
        //图片放大
        $scope.img_big=false;
        // $scope.img_click = function(imgSrc){
        //     console.log(1)
        //     if(imgSrc){
        //         $scope.imgSrc = imgSrc;
        //     }
        //     $scope.img_big=!$scope.img_big;
        //
        //     $scope.scaleImg=imgSrc;
        //     var imgObj = new Image();
        //     imgObj.src = imgSrc; //若是jq，则直接将此代码换成 imgObj.src = _test.attr("src"); 即可。
        //     var k = document.body.clientHeight/2-imgObj.width/2;
        //     console.log(k)
        //     $(".bigImgBox").css("top",k+'px');
        // }
        //图片放大结束
        $scope.tipShow = false;//弹框
		$scope.curriculum_id = $stateParams.curriculum_id;
		$scope.curriculum_name = $stateParams.curriculum_name;
		$scope.courseIndex = parseInt($stateParams.index);
		$scope.num = 0;
        $scope.span_right = false;
		$rootScope.bstop = true;
		//判断是多选还是单选
        $scope.ques_type = $cookieStore.get('refurbish')==1?1:2;
		$scope.tipModal = {
			isShow: false,
			title: '温馨提示',
			data: {},
			confirm: function() {
				this.hide()
			},
			cancel: function() {
				this.hide();
			},
			hide: function() {
				this.isShow = false;
			},
			show: function() {
				this.isShow = true;
			}
		}
		if($rootScope.num==0){
			$scope.tipModal.show();
			$rootScope.num=1;
		}
		$scope.gostudy = function() {
				var url = $state.href('app.compProfession');
				window.location.href = url;				
			}
        $scope.load = {
			_getchapterList: function() { //章节
				$http({
					url: app._baseUrl + '/Qy/Task/chapterList&curriculum_id=' + $scope.curriculum_id,
					method: 'GET',
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					}
				}).success(function(responed) {
                    $scope.loading_show = false;
					$scope.chapterList = responed.data;
					$rootScope.secondary = false;
					angular.forEach($scope.chapterList, function(v, k) {
						v.index = k
					});
					$rootScope.chapter_id = $scope.chapterList[0].chapter_id;
					$http({
						url: app._baseUrl + '/Qy/Task/periodList',
						method: 'POST',
						data:{
                            chapter_id:$scope.chapterList[0].chapter_id,
                            task_type:$scope.ques_type
						},
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
					}).success(function(responed) {
						$scope.periodList = responed.data;
						$rootScope.secondary = false;
						angular.forEach($scope.periodList, function(v, k) {
							v.index = k
						});
					});
				});

			},
			init: function() {
				$scope.chapterList = [];
				this._getchapterList && this._getchapterList();
			}
		};
		$scope.load.init && $scope.load.init();

		$scope.chapterlist = function(item, index) {
			$scope.temp = index;
			$scope.num = 1;
			$rootScope.chapter_id = item.chapter_id;

			$http({
				url: app._baseUrl + '/Qy/Task/periodList',
				method: 'POST',
				data:{
                    chapter_id:item.chapter_id,
                    task_type:$scope.ques_type
				},
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				}
			}).success(function(responed) {
				$scope.periodList = responed.data;
				$rootScope.secondary = false;
				angular.forEach($scope.periodList, function(v, k) {
					v.index = k
				});
			});
		};

		//做题弹窗
		$scope.questionPop = function(item) {
            $scope.color = false;
            $scope.ti_index = 0;
           // console.log($scope.ques_type);
			if($scope.ques_type==1){
                $scope.answering = true;
                $scope.finish = false;
                $rootScope.period_id = item.period_id;
                //获取题目列表
                $http({
                    url: app._baseUrl + '/Qy/Task/questionList',
                    method: 'POST',
                    data: {
                        period_id: $rootScope.period_id,
                        task_type:$scope.ques_type
                        // task_type:1
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(responed) {
                    if(responed.success){
                        //console.log(responed.data)
                        $scope.questionModal.show();
                        $scope.questionList = responed.data;
                        $scope.questionlist = $scope.questionList.question_list;
                        for(i = 0; i < $scope.questionlist.length; i++) {//未作答就从1开始作答
                            $scope.check_score = $scope.questionlist[i].check_score;
                            if($scope.check_score == -1) {
                                questionindex = i;
                                break
                            }
                            if($scope.check_score.indexOf(-1) == -1) {
                                questionindex = 0;
                            }
                        }
                        $rootScope.secondary = false;
                        $rootScope.listall = $scope.questionlist.length;
                        $scope.now_right_count = $scope.questionList.now_right_count;
                        $scope.now_wrong_count =$scope.questionList.now_wrong_count;
                       /* $scope.now_score = $scope.questionList.now_right_score;*/
                        $scope.paper_id = $scope.questionList.paper_id;
                        //区分单选还是多选再去累计题目
                        if($scope.ques_type==1){
                            $rootScope.question = questionindex;
                            $scope.questionModal.title = "第" + parseInt($rootScope.question + 1) + "题";
                        }
                        // else{
                        //     $rootScope.question_duo = questionindex;
                        //     $scope.questionModal.title = "第" + parseInt($rootScope.question_duo + 1) + "题";
                        // }

                    }else{
                        $scope.sec = 5;
                        var interval = setInterval(function() {//设置定时器
                            if($scope.sec > 0) {
                                $scope.sec--;
                                $scope.tipShow = true;//提示显示
                                $scope.tipmess = '该课时还没有随堂任务';//将提示信息绑定
                                $('.black_patch').fadeIn(600);
                            } else {
                                clearInterval(interval);
                                $scope.tipShow = true;
                                $('.black_patch').fadeOut(600);
                            }
                            $scope.$digest();//脏数据检验
                        }, 500)
                    }
                });
                $scope.answer = function(k, item) {
                    if($rootScope.bstop == true) {
                        $http({
                            url: app._baseUrl + '/Qy/Task/answerCheck',
                            method: 'POST',
                            data: {
                                paper_id: $scope.paper_id,
                                question_id: item.question_id,
                                s_answer: k
                            },
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        }).success(function(responed) {
                                $rootScope.bstop = false;
                                $scope.answerList = responed.data;
                                $rootScope.secondary = false;
                                $scope.numb = 0;
                                $scope.check_score = $scope.answerList.check_score;
                                $scope.questionList.now_right_score=responed.data.total_score;
                                    $scope.now_star_num = responed.data.star_num;
                                if($scope.check_score > 0) {//正确
                                    $scope.questionList.now_right_count += 1;
                                    $scope.score = parseInt($scope.check_score);
                                  /*  $scope.questionList.now_right_score += $scope.score;*/
                                }else if($scope.check_score ==0){//错误
                                    $scope.questionList.now_wrong_count+=1;
                                }
                                var gitanswer = function () {
                                    if($scope.ques_type==1){
                                        $rootScope.question += 1;
                                        $scope.questionModal.title = "第" + parseInt($rootScope.question + 1) + "题";
                                        if($rootScope.question == $rootScope.listall) {
                                            $scope.answering = false;
                                            $scope.finish = true;
                                            $scope.questionModal.title = "";
                                        }
                                    }
                                    $scope.numb = 1;
                                    $rootScope.bstop = true;

                                };
                                $timeout(gitanswer,1000);
                            }
                        );
                        return false
                    }
                }
			}else{//多选题
				//console.log(item)
				$scope.finish = false;
				$scope.xianshi_defen = 0;
				//课时id
				$scope.keshi_id = item.period_id;
                $http({//题目列表
                    url: app._baseUrl + '/Qy/Task/questionList',
                    method: 'POST',
                    data: {
                        period_id: $scope.keshi_id,
                        task_type:$scope.ques_type
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(res){
                	if(res.success){
                        //console.log(res.data);
                        $scope.data_duo = res;
                        //console.log($scope.data_duo);
                        $scope.now_right_count = res.data.now_right_count;//目前答对题数
                        $scope.now_wrong_count = res.data.now_wrong_count;//目前答错题数
                       /* $scope.now_right_score = res.data.now_right_score;//目前分数*/
                        $scope.question_list = res.data.question_list;
                        $scope.len = $scope.question_list.length;//多选总数
                        for(i = 0; i < $scope.len; i++) {//未作答就从1开始作答
                            $scope.check_score_duo = $scope.question_list[i].check_score;
                            if($scope.check_score_duo == -1) {
                                questionindex_duo = i;
                                break
                            }
                            if($scope.check_score_duo.indexOf(-1) == -1) {
                                questionindex_duo = 0;
                            }
                        }
                        $rootScope.timu_index = questionindex_duo;
                        //开始时显示第一题索引
                        // $scope.timu = 0;
                        $scope.timu = $rootScope.timu_index;
                        $scope.timu_id = $scope.question_list[$scope.timu].question_id;
                        $scope.paper_id = res.data.paper_id;
                        // $scope.fenzhi = $scope.question_list[$scope.timu].score;
                        //显示做题弹窗
                        $scope.questionModal.isShow = true;
                        //$scope.answer_kaishi = true;
                    }else{
                        $scope.sec = 5;
                        var interval = setInterval(function() {//设置定时器
                            if($scope.sec > 0) {
                                $scope.sec--;
                                $scope.tipShow = true;//提示显示
                                $scope.tipmess = '该课时还没有进阶任务';//将提示信息绑定
                                $('.black_patch').fadeIn(600);
                            } else {
                                clearInterval(interval);
                                $scope.tipShow = true;
                                $('.black_patch').fadeOut(600);
                            }
                            $scope.$digest();//脏数据检验
                        }, 500)
                    }

				})
			}

		};

        // $scope.questionPop = function(item,parentIndex,index1){
        //     $scope.questionModal.show();
        //     $scope.finish = true;
        // }
		$scope.statego = function () {
			history.back()
		};
		// 弹框
		$scope.questionModal = {
			isShow: false,
			title: '',
			data: {},
			confirm: function() {
				$scope.answering = true;
				$scope.finish = false;
				$http({
					url: app._baseUrl + '/Qy/Task/questionList',
					method: 'POST',
					data: {
						period_id: $rootScope.period_id,
                        task_type:$scope.ques_type
					},
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					}
				}).success(function(responed) {
					$scope.questionList = responed.data;
					$scope.questionlist = $scope.questionList.question_list;
					for(i = 0; i < $scope.questionlist.length; i++) {
						$scope.check_score = $scope.questionlist[i].check_score;
						if($scope.check_score == -1) {
							questionindex = i;
							break
						}
						if($scope.check_score.indexOf(-1) == -1) {
							questionindex = 0;
						}
					}
					$rootScope.secondary = false;
					$scope.listall = $scope.questionlist.length;
					$scope.now_right_count = $scope.questionList.now_right_count;
                    $scope.now_wrong_count = $scope.questionList.now_wrong_count;
					/*$scope.now_score = $scope.questionList.now_right_score;*/
					$scope.paper_id = $scope.questionList.paper_id;
					$rootScope.question = questionindex;
					$scope.questionModal.title = "第" + parseInt($rootScope.question + 1) + "题";
				});
			},
			cancel: function() {
                $http({
                    url: app._baseUrl + '/Qy/Task/periodList',
                    method: 'POST',
                    data:{
                        chapter_id:$rootScope.chapter_id,
                        task_type:$scope.ques_type
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(responed) {
					$scope.periodList = responed.data;
					$rootScope.secondary = false;
					angular.forEach($scope.periodList, function(v, k) {
						v.index = k
					});
				});
				this.hide();
			},
			hide: function() {
				this.isShow = false;
			},
			show: function() {
				this.isShow = true;
			},
            video:function(){
			    this.cancel();
                var url = $state.href('app.trainCourse',{curriculum_id:$scope.curriculum_id});
                window.open(url,'_blank');
            },
            nowvideo:function(){
                this.cancel();
                var url = $state.href('app.trainCourse',{curriculum_id:$scope.curriculum_id});
                window.open(url,'_blank');
            }
		}
        //多选
        $scope.arr_save_xuanxiang = [];
        $scope.xianshi_defen = 0;//显示的得分初始值0;
        $scope.duo_xuan = function(par_index,index,color,ques_id2){
            index = aBcd(index);
            if(!$scope.arr_save_xuanxiang[par_index]){//做的是新的一道多选题时
                if(color==true){
                    $scope.arr_save_xuanxiang[par_index] = {//将此题信息保存
                        ques_id:ques_id2,
                        temp:[index]
                    }
                    //console.log($scope.arr_save_xuanxiang[par_index].temp)
                }
            }else{//还是选择同一题
                $scope.arr_save_xuanxiang[par_index].temp = $scope.arr_save_xuanxiang[par_index].temp.split(',');
                if(color==true){
                    $scope.arr_save_xuanxiang[par_index].temp.push(index);
                }else{
                    $scope.arr_save_xuanxiang[par_index].temp.remove(index);
                }
                var len = $scope.arr_save_xuanxiang[par_index].temp.length;
                for(var i = 0; i < len; i++) {
                    if($scope.arr_save_xuanxiang[par_index].temp[i] == '') $scope.arr_save_xuanxiang[par_index].temp.splice(i,1);
                }
            }
            //排序
            var arr1 = $scope.arr_save_xuanxiang[par_index].temp;
            $scope.arr_save_xuanxiang[par_index].temp = arr1.sort().join(',');
			$scope.kehu_answer = $scope.arr_save_xuanxiang[par_index].temp;
        }
		//下一题一个点击事件
        $scope.answer_kaishi = true;
		$scope.xiayiti = function(){
				if(!$scope.kehu_answer){
					return false;
				}
        		$scope.answer_kaishi = true;
                //题目id
                $scope.timu_id = $scope.question_list[$scope.timu].question_id;
                $http({//提交答案
                    url: app._baseUrl + '/Qy/Task/answerCheck',
                    method: 'POST',
                    data:{
                        paper_id:$scope.paper_id,
                        question_id:$scope.timu_id,
                        s_answer:$scope.kehu_answer
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(res){
                    if(res.success){
                        //console.log(res);
                       // console.log(res.data.total_score)
                        // $scope.span_right = true;
                        //正确选项
                        $scope.zhengque_answer = res.data.answer;
                        // if(res.data.total_score!=null){
                        //     $scope.questionList.now_right_score=res.data.total_score;
                        // }
                        $scope.now_right_score = res.data.total_score
                        $scope.span_right = true;
                        //判断答案真确
                        if(res.data.check_score>0){
                           /* $scope.defen = parseInt($scope.question_list[$scope.timu].score);
                            $scope.xianshi_defen+=$scope.defen;
                            $scope.now_right_score = $scope.xianshi_defen;*/
                            $scope.now_right_count++;
                        }else if(res.data.check_score==0){
                            $scope.now_wrong_count++;
                        }
                            //设置2秒
                            // $scope.timu_id = $scope.question_list[$scope.timu++].question_id;
                        var interval = setTimeout(function() {//设置定时器
                            if($scope.timu<$scope.len-1){
                                clearTimeout(interval);
                                $scope.timu++;
                                $scope.timu_id = $scope.question_list[$scope.timu].question_id;
                                $scope.fenzhi = $scope.question_list[$scope.timu].score;
                                $scope.zhengque_answer = '';
                                $scope.span_right = false;
                            }else{
                                $scope.answer_kaishi = false;
                                $scope.finish = true;
                                $scope.timu = 0;
                                $scope.color = false;
                                $(".questionr_con li").removeClass('pitch_bg');
                            }
                            $scope.$digest();//脏数据检验
                        }, 2000)
                        $scope.kehu_answer = '';
                    }
                })
			}
			//关闭
        $scope.close_tc = function(){
            $scope.questionModal.isShow = false;
            $scope.arr_save_xuanxiang = [];
            $scope.kehu_answer = '';
            $scope.finish = false;
            $scope.answer_kaishi = true;
            $scope.xianshi_defen = 0;
            $scope.zhengque_answer = '';
            $scope.questionModal.cancel();
        }

        //再做一遍
        $scope.again_duo = function(){//不能重新提交
            $scope.arr_save_xuanxiang = [];
            $scope.kehu_answer = '';
		    $scope.finish = false;
		    $scope.answer_kaishi = true;
            $scope.xianshi_defen = 0;
            $scope.zhengque_answer = '';
            //课时id
            // $scope.keshi_id = item.period_id;
            $http({//题目列表
                url: app._baseUrl + '/Qy/Task/questionList',
                method: 'POST',
                data: {
                    period_id: $scope.keshi_id,
                    task_type:$scope.ques_type
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).success(function(res){
                //console.log(res);
                $scope.data_duo = res;
                $scope.now_right_count = res.data.now_right_count;
                $scope.now_wrong_count = res.data.now_wrong_count;
               /* $scope.now_right_score = res.data.now_right_score;*/
                $scope.question_list = res.data.question_list;
                $scope.len = $scope.question_list.length;//多选总数
                //开始时显示第一题索引
                $scope.timu = 0;
                $scope.timu_id = $scope.question_list[$scope.timu].question_id;
                $scope.paper_id = res.data.paper_id;
                $scope.fenzhi = $scope.question_list[$scope.timu].score;

            })

        };


        Array.prototype.remove = function(val) {//数组去掉特定项
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };
        function aBcd(index){
            if(index==0){
                return 'A';
            }
            if(index==1){
                return 'B';
            }
            if(index==2){
                return 'C';
            }
            if(index==3){
                return 'D';
            }
            if(index==4){
            	return 'E'
			}
        }
        //图片放大
        $scope.img_big=false;
        $scope.img_click = function(img){
            $scope.img_big=!$scope.img_big;
            $scope.scaleImg=img;
            var imgObj = new Image();
            imgObj.src = img; //若是jq，则直接将此代码换成 imgObj.src = _test.attr("src"); 即可。
            var k = document.body.clientWidth/2-imgObj.width/2;
            var v = document.body.clientHeight/2-imgObj.height/2;
            $(".bigImgBox").css("left",k+'px');
            $(".bigImgBox").css("top",v+'px');
            $scope.obj_img = {
                left:k,
                top:v,
                width:imgObj.width,
                height:imgObj.height
            }
            $(".bigImgBox").css("left",$scope.obj_img.left+'px');
            $(".bigImgBox").css("top",$scope.obj_img.top+'px');
            $('.bigImgBox .bigImg').css({'width':$scope.obj_img.width,"height":$scope.obj_img.height});
        }
        //图片放大结束
        var divImg = document.getElementsByClassName('bigImgBox');
        divImg[0].addEventListener("DOMMouseScroll",function(e){
            var imgWidth = $('.bigImgBox .bigImg').width();
            var imgHeight = $('.bigImgBox .bigImg').height();
            var imgLeft = divImg[0].offsetLeft;
            var imgTop = divImg[0].offsetTop;
            //console.log(e)
            if(e.detail > 0){
                imgLeft = imgLeft-imgWidth/20;
                imgTop = imgTop-imgHeight/20;
                imgWidth = imgWidth+imgWidth/10;
                imgHeight = imgHeight+imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
            if(e.detail < 0){
                imgLeft = imgLeft+imgWidth/20;
                imgTop = imgTop+imgHeight/20;
                imgWidth = imgWidth-imgWidth/10;
                imgHeight = imgHeight-imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
        });

       divImg[0].addEventListener('mousewheel',function(e){
           var imgWidth = $('.bigImgBox .bigImg').width();
           var imgHeight = $('.bigImgBox .bigImg').height();
           var imgLeft = divImg[0].offsetLeft;
           var imgTop = divImg[0].offsetTop;
           if(e.deltaY > 0 || e.wheelDelta < 0){
               imgLeft = imgLeft-imgWidth/20;
               imgTop = imgTop-imgHeight/20;
               imgWidth = imgWidth+imgWidth/10;
               imgHeight = imgHeight+imgHeight/10;
               $('.bigImgBox').css({'left':imgLeft,'top':imgTop});
               $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
           }
           if(e.deltaY < 0|| e.wheelDelta > 0){
               imgLeft = imgLeft+imgWidth/20;
               imgTop = imgTop+imgHeight/20;
               imgWidth = imgWidth-imgWidth/10;
               imgHeight = imgHeight-imgHeight/10;
               $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
               $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
           }
       })

	})
});